import bat.ke.qq.com.AppConfig;
import bat.ke.qq.com.bean.Fox;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.*;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.util.Arrays;

/**
 * 源码学院-Fox
 * 只为培养BAT程序员而生
 * http://bat.ke.qq.com
 * 往期视频加群:516212256 暗号:6
 */
public class IoCTest2 {

    @Test
    public void test(){

        ApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
        // NoSuchBeanDefinitionException
        context.getBean("user");
    }


    @Test
    public void testXml(){

//        ApplicationContext  context =  new ClassPathXmlApplicationContext("spring.xml");
//        //   beanDefinitionMap   beanName: beanDefinition
//        System.out.println(Arrays.toString(context.getBeanDefinitionNames()));
//
//        System.out.println(context.getBean("user"));

        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("spring.xml");

        //注册
       // BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
        DefaultListableBeanFactory registry = new DefaultListableBeanFactory();
        BeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);
        reader.loadBeanDefinitions(resource);

        System.out.println(Arrays.toString(registry.getBeanDefinitionNames()));

        //注册 beanDefinition
        BeanDefinition beanDefinition = new RootBeanDefinition(Fox.class);
         beanDefinition.getPropertyValues().add("name","fox老师");
        //beanDefinition.setBeanClassName("bat.ke.qq.com.bean.User");
        // beanDefinitionMap.put("fox",beanDefinition)
        registry.registerBeanDefinition("fox",beanDefinition);

        System.out.println(Arrays.toString(registry.getBeanDefinitionNames()));

        // Fox  beanFactory   最最最核心： DefaultListableBeanFactory
        System.out.println(registry.getBean("userService"));  // Fox  User
    }
}
